package org.codert.jpa.domain.order;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author TanJingyu
 */
public interface OrderRepository extends JpaRepository<Order, Long>, JpaSpecificationExecutor<Order> {

    @Query(value = "select * from orders o", nativeQuery = true,
            countQuery = "select count(*) from orders o")
    Page<Order> pageHHHH(Pageable pageable);

//    @Query(nativeQuery = true, value = "select o.amount amount, u.username username from orders o left join user u on o.user_id = u.id")
    @Query(value = "select o from orders o")
    List<OrderProjection> projection();

    interface OrderProjection {
        BigDecimal getAmount();
        UserProjection getUser();

        interface UserProjection {
            String getUsername();
        }
    }
}
